<template>
  <div>
    <div class="table-box">
      <el-table
        v-loading="loading"
        stripe
        :data="tableData"
        element-loading-spinner="el-icon-loading"
        element-loading-background="rgba(255, 255, 255, 0.8)"
        @selection-change="onSelect"
      >
        <el-table-column
          v-for="item in columns"
          :key="item.prop"
          :prop="item.prop"
          :label="item.label"
          align="center"
        />
        <el-table-column label="操作" fixed="right" width="160px">
          <!--            操作，场景0	无条件显示	详情，涉及接口：数据应用发布详情-->
          <!--            操作，场景1	status=online	下架，涉及接口：上架/下架-->
          <!--            操作，场景2	status=offline	上架，涉及接口：上架/下架-->
          <!--            操作，场景3	status=rejection	重新申请，涉及接口：修改数据应用-->
          <!--            操作，场景4	status=default	编辑，涉及接口：修改数据应用-->
          <!--            操作，场景5	status=in_review 且 notStartAudit = false	撤销审核，涉及接口：撤销审核-->
          <!--            操作，场景6	status=in_review 且 notStartAudit = true	编辑(页面隐藏保存并提交按钮)、撤销审核，涉及接口：修改数据应用、撤销审核-->
          <template slot-scope="scope">
            <el-button
              type="text"
              @click="dialogDetail(scope.row)"
            >详情</el-button>
            <el-button
              v-if="scope.row.status === 'online'"
              type="text"
              @click="handleChangeUp(scope.row, false,)"
            >下架</el-button>
            <el-button
              v-if="scope.row.status === 'offline'"
              type="text"
              @click="handleChangeUp(scope.row, true,)"
            >上架</el-button>
            <el-button
              v-if="scope.row.status === 'rejection'"
              type="text"
              @click="dialogEdit(scope.row, true, 'rejection')"
            >重新申请</el-button>
            <el-button
              v-if="scope.row.status === 'default'"
              type="text"
              @click="dialogEdit(scope.row)"
            >编辑</el-button>
            <el-button
              v-if="scope.row.status === 'in_review' && !scope.row.notStartAudit"
              type="text"
              @click="handleRevoke(scope.row)"
            >撤销审核</el-button>
            <el-button
              v-if="scope.row.status === 'in_review' && scope.row.notStartAudit"
              type="text"
              @click="dialogEdit(scope.row)"
            >编辑</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="page-box">
      <el-pagination
        background
        :current-page="pageNo"
        :page-sizes="pageSizes"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="totalSize"
        @size-change="changePageSize"
        @current-change="changePageNo"
      />
    </div>
    <api-add-form ref="apiAddForm" title="接口发布申请" @save="initData" />
  </div>
</template>

<script>
import crud from '@/mixins/crud'
import { dict_openTypeList, dict_shareNatureList, dict_shareTypeList } from '@/dict'
import { mapGetters } from 'vuex'
import { http_serve_audit_process, http_serve_publish_data } from '@/api'
import ApiAddForm from './addForm'
export default {
  components: {
    ApiAddForm
  },
  mixins: [crud],
  computed: {
    ...mapGetters(['enum']),
    statusList() {
      return this.enum['PublishDataStatusType'] || []
    }
  },
  data() {
    return {
      request: {
        ...http_serve_publish_data,
        create: http_serve_publish_data.create_api,
        update: http_serve_publish_data.update_api
      },
      columns: [
        { prop: 'createTime', label: '申请时间' },
        { prop: 'code', label: '申请单号' },
        { prop: 'name', label: '服务名称' },
        { prop: 'shareTypeName', label: '共享类型' },
        { prop: 'openTypeName', label: '开放类型' },
        { prop: 'shareNatureName', label: '目录类型' },
        { prop: 'statusName', label: '服务状态' },
        { prop: 'onlineTime', label: '上架时间' }
      ]
    }
  },
  methods: {
    getQueryParams() {
      const params = {
        pageNum: this.pageNo,
        pageSize: this.pageSize,
        type: 'api'
      }
      for (const paramsKey in this.params) {
        if (paramsKey === 'timeValue') {
          if (!this.params[paramsKey] || !this.params[paramsKey].length) {
            this.params[paramsKey] = ['', '']
          }
          params['startTime'] = this.params.timeValue[0] || undefined
          params['endTime'] = this.params.timeValue[1] || undefined
        } else {
          params[paramsKey] = this.params[paramsKey]
        }
      }
      return params
    },
    formatTableData(data) {
      data.forEach(v => {
        v.shareTypeName = dict_shareTypeList.find(el => el.value == v.shareType) && dict_shareTypeList.find(el => el.value == v.shareType).name
        v.openTypeName = dict_openTypeList.find(el => el.value == v.openType) && dict_openTypeList.find(el => el.value == v.openType).name
        v.shareNatureName = dict_shareNatureList.find(el => el.value == v.shareNature) && dict_shareNatureList.find(el => el.value == v.shareNature).name
        v.statusName = this.statusList.find(el => el.value == v.status) && this.statusList.find(el => el.value == v.status).name
      })
    },
    // 上架/下架
    handleChangeUp(rowItem, value) {
      this.request.upOrDown({
        id: rowItem.id
      }, {
        online: value
      }).finally(() => {
        this.initData()
      })
    },
    // 撤销审核
    handleRevoke(rowItem) {
      this.$confirm('确认撤销审核？', '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          http_serve_audit_process.revoke({
            id: rowItem.id
          }, {}).finally(() => {
            this.initData()
          })
        })
    },
    // 详情
    dialogDetail(item) {
      this.$router.push({
        path: '/serve-publish/server-detail/publish/' + item.id + '/' + (item.auditRequestId || '')
      })
    },
    // 编辑
    dialogEdit(rowItem) {
      this.$refs.apiAddForm.openEdit(rowItem)
    },
    openDraw() {
      console.log('dada')
      this.$refs.apiAddForm.open()
    }
  }
}
</script>

<style scoped>

</style>
